package com.server.sercurity.exception;

import com.server.common.api.ApiResult;
import com.server.common.enums.ResultCodeEnum;
import com.server.common.exception.GolbalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;


/**
 * @author zgr
 * @date 2023/7/21 15:03
 * @description
 */

@Slf4j
public class AdminExceptionHandler extends GolbalExceptionHandler {

    @ExceptionHandler(value = AuthException.class)
    @ResponseStatus(HttpStatus.OK)
    public ApiResult AuthException(AuthException e) {
        log.error("权限异常！请求异常：{}", e.getMessage());
        return ApiResult.fail(ResultCodeEnum.Fail.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = UsernameNotFoundException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ApiResult defaultException(UsernameNotFoundException e) {
        log.error("请求异常：{}", e.getMessage());
        return ApiResult.fail(ResultCodeEnum.Fail.getCode(), e.getMessage());
    }
}
